﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoccerSimulator5000
{
    public class Team : IComparable<Team>
    {
        public string name { get; set; }

        public int bestEndPosition { get; set; }
        public int worstEndPosition { get; set; }



        public StandingsData standingsData;
        public TeamData teamData;
        public Finance finance = new Finance();

        public Team()
        {
            //name = null;
            //teamData = new TeamData();

        }

        // Part of attempt to solve load-issue
        public void Init()
        {

        }

        public void CalculateSalaries()
        {
            int tempSalaries = 0;

            foreach (var Footballer in teamData.squad.squad)
            {
                tempSalaries += Footballer.wage;
            }

            finance.totalSalaries = tempSalaries;
        }

        public Team(string name, float forward, float midfield, float defender, float goalie)
        {
            this.name = name;
            standingsData = new StandingsData();
            teamData = new TeamData();

            teamData.forwardScore = forward;
            teamData.midfieldScore = midfield;
            teamData.defenderScore = defender;
            teamData.goalieScore = goalie;
            teamData.UpdateData();
            bestEndPosition = 1;
            worstEndPosition = 12;
        }

        public Team(string name)
        {
            this.name = name;
            standingsData = new StandingsData();
            teamData = new TeamData();
        }

        //public void CalculatePossibleEndPositions()
        //{
        //    int totalRounds = (Game.teamList.Count() - 1) * 2;
        //    int gamesLeft = totalRounds - Game.player.playerTeam.standingsData.gamesPlayed;
        //    int playerTeamMaxPoints = gamesLeft * 3 + Game.player.playerTeam.standingsData.points;

        //    if (Game.player.playerTeam.standingsData.gamesPlayed != 0)
        //    {
        //        //Calculate best possible position (dreamScenario(tm), only semi-possible)
        //        for (int i = 1; i < Game.player.playerTeam.standingsData.position; i++)
        //        {
        //            if (Game.season.seasonStandings.standings[i].standingsData.points <= playerTeamMaxPoints)
        //            {
        //                bestEndPosition = Game.season.seasonStandings.standings[i].standingsData.position;
        //            }
        //        }

        //         //Calculate worst possible position (dreamScenario(tm), only semi-possible)
        //        for (int i = Game.player.playerTeam.standingsData.position + 1; i < Game.teamList.Count(); i++)
        //        {
        //            int opponentTeamMaxPoints = gamesLeft * 3 + Game.season.seasonStandings.standings[i].standingsData.points;
        //            if (Game.player.playerTeam.standingsData.points < opponentTeamMaxPoints)
        //            {
        //                worstEndPosition = Game.season.seasonStandings.standings[i].standingsData.position;
        //            }
        //        }
        //    }
        //}

        public void Draw(System.Drawing.Graphics g, int x, int y)
        {
            int fontSize = 10;
            g.DrawString( ("Team: " + name), new System.Drawing.Font(System.Drawing.FontFamily.GenericSansSerif, fontSize), System.Drawing.Brushes.Black, new System.Drawing.PointF(x, y));
            teamData.Draw( g, x, y+fontSize );
        }

        public int CompareTo( Team other)
        {
            if (this.standingsData.points < other.standingsData.points) return 1;
            else if (this.standingsData.points > other.standingsData.points) return -1;
            else if (this.standingsData.points == other.standingsData.points)
            {
                if (this.standingsData.goalsDiff < other.standingsData.goalsDiff) return 1;
                else if (this.standingsData.goalsDiff > other.standingsData.goalsDiff) return -1;
                else if (this.standingsData.goalsDiff == other.standingsData.goalsDiff)
                {
                    if (this.standingsData.goalsFor < other.standingsData.goalsFor) return 1;
                    else if (this.standingsData.goalsFor > other.standingsData.goalsFor) return -1;
                    else if (this.standingsData.goalsFor == other.standingsData.goalsFor)
                    {
                        if (this.standingsData.goalsAgainst > other.standingsData.goalsAgainst) return 1;
                        else if (this.standingsData.goalsAgainst < other.standingsData.goalsAgainst) return -1;
                        else return 0;
                    }
                    else return 0;
                }
                else return 0;
            }
            else return 0;

            
        }

    }
}
